﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;

namespace Client
{
    public partial class Form1 : Form, IHostFunctionsCallback
    {
        HostFunctionsClient client = null;

        public Form1()
        {
            InitializeComponent();

            InstanceContext context = new InstanceContext(this);
            client = new HostFunctionsClient(context);
        }

        private void btn_Connect_Click(object sender, EventArgs e)
        {
            client.connect();
            this.txtBx_Name.Enabled = false;
            this.btn_Send.Enabled = true;
            this.btn_Connect.Enabled = false;
            this.btn_Disconnect.Enabled = true;
        }

        private void btn_Disconnect_Click(object sender, EventArgs e)
        {
            client.disconnect();
            this.txtBx_Name.Enabled = true;
            this.btn_Send.Enabled = false;
            this.btn_Connect.Enabled = true;
            this.btn_Disconnect.Enabled = false;
        }

        private void btn_Send_Click(object sender, EventArgs e)
        {
            Datalib.ChatMessage cm = new Datalib.ChatMessage();
            cm.Name = this.txtBx_Name.Text;
            cm.Message = this.txtBx_Message.Text;
            if (client.State == CommunicationState.Opened)
            {
                client.send(cm);
            }
            DisplayMessage(this.txtBx_Name.Text + ":\t" + this.txtBx_Message.Text);
            this.txtBx_Message.Clear();
        }

        #region IHostFunctionsCallback Member

        public void ShowMessage(string message)
        {
            DisplayMessage(message);
        }

        public void DisplayMessage(string message)
        {
            this.txtBx_chat.Text = this.txtBx_chat.Text + "\r\n" + message;
            this.txtBx_chat.SelectionStart = this.txtBx_chat.Text.Length;
            this.txtBx_chat.ScrollToCaret();
        }

        #endregion
    }
}
